package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppSite;
import com.ruoyi.background.mapper.AppSiteMapper;
import com.ruoyi.background.service.IAppGradeRecordService;
import com.ruoyi.background.service.IAppSiteService;
import com.ruoyi.background.utils.REDIS_KEY;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 知识关卡Service业务层处理
 *
 * @author susu
 * @date 2024-08-10
 */
@Service
public class AppSiteServiceImpl implements IAppSiteService {

    private static final Logger log = LoggerFactory.getLogger(AppSiteServiceImpl.class);

    @Autowired
    private AppSiteMapper appSiteMapper;

    @Autowired
    private IAppGradeRecordService gradeRecordService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询知识关卡
     *
     * @param id 知识关卡主键
     * @return 知识关卡
     */
    @Override
    public AppSite selectAppSiteById(Long id) {
        return appSiteMapper.selectAppSiteById(id);
    }

    /**
     * @param userId 用户id
     * @return false true
     */
    @Override
    public boolean userIsFinalSite(Long userId) {
        Integer userCountInGrade = gradeRecordService.getUserCountInGrade(userId);
        AppSite site = appSiteMapper.getUserNextSite(userCountInGrade);
        String key = REDIS_KEY.APP_USER_FINAL_FLAG_KEY + userId;
        int flag = ObjectUtils.isEmpty(site) ? 1 : 2;
        redisCache.setCacheObject(key, flag);
        return ObjectUtils.isEmpty(site);
    }

    /**
     * 获取用户下一关卡
     *
     * @return 结果
     */
    @Override
    public AppSite getUserNextSite() {
        Integer userCountInGrade = gradeRecordService.getUserCountInGrade();
        AppSite site = appSiteMapper.getUserNextSite(userCountInGrade);
        if (ObjectUtils.isEmpty(site)) {
            log.info("用户：" + SecurityUtils.getUserId() + " 到达最后一个关卡");
            String key = REDIS_KEY.APP_USER_FINAL_FLAG_KEY + SecurityUtils.getUserId();
            redisCache.setCacheObject(key, 1);
        } else {
            log.info("用户：" + SecurityUtils.getUserId() + " 没有到达最后一个关卡");
            String key = REDIS_KEY.APP_USER_FINAL_FLAG_KEY + SecurityUtils.getUserId();
            redisCache.setCacheObject(key, 2);
        }
        return site;

    }

    /**
     * 获取用户上一关卡
     *
     * @return 结果
     */
    @Override
    public AppSite getUserBeforeSite() {
        Integer userCountInGrade = gradeRecordService.getUserCountInGrade();
        return appSiteMapper.getUserBeforeSite(userCountInGrade);
    }

    /**
     * 查询知识关卡列表
     *
     * @param appSite 知识关卡
     * @return 知识关卡
     */
    @Override
    public List<AppSite> selectAppSiteList(AppSite appSite) {
        return appSiteMapper.selectAppSiteList(appSite);
    }

    /**
     * 更新缓存的列表
     */
    @Async
    @Override
    public void updateSiteCacheList() {
        String key = REDIS_KEY.SITE_LIST_KEY;
        List<AppSite> siteList = appSiteMapper.selectAppSiteList(new AppSite());
        redisCache.deleteByKey(key);
        redisCache.setCacheList(key, siteList);
        redisCache.expire(key, 60, TimeUnit.MINUTES);
    }

    /**
     * 获取下拉选择
     *
     * @return 结果
     */
    @Override
    public List<AppSite> selectAppSiteSelect() {
        return appSiteMapper.selectAppSiteSelect();
    }

    /**
     * 新增知识关卡
     *
     * @param appSite 知识关卡
     * @return 结果
     */
    @Override
    public int insertAppSite(AppSite appSite) {
        appSite.setCreateTime(DateUtils.getNowDate());
        appSite.setCreateBy(SecurityUtils.getUsername());
        appSite.setUpdateBy(SecurityUtils.getUsername());
        appSite.setUpdateTime(DateUtils.getNowDate());
        int res = appSiteMapper.insertAppSite(appSite);
        if (res > 0) {
            this.updateSiteCacheList();
        }
        return res;
    }

    /**
     * 修改知识关卡
     *
     * @param appSite 知识关卡
     * @return 结果
     */
    @Override
    public int updateAppSite(AppSite appSite) {
        AppSite site = appSiteMapper.selectAppSiteById(appSite.getId());
        if (ObjectUtils.isEmpty(site)) throw new RuntimeException("对象不存在");
        if (1 == site.getDelFlag()) throw new RuntimeException("对象已删除");
        appSite.setUpdateTime(DateUtils.getNowDate());
        appSite.setUpdateBy(SecurityUtils.getUsername());
        int res = appSiteMapper.updateAppSite(appSite);
        if (res > 0) {
            this.updateSiteCacheList();
        }
        return res;
    }

    /**
     * 修改状态
     *
     * @param id     id
     * @param status 状态
     * @return 结果
     */
    @Override
    public int changeStatus(Long id, Integer status) {
        AppSite site = appSiteMapper.selectAppSiteById(id);
        if (ObjectUtils.isEmpty(site)) throw new RuntimeException("对象不存在");
        if (1 == site.getDelFlag()) throw new RuntimeException("对象已删除");
        AppSite appSite = new AppSite();
        appSite.setId(id);
        appSite.setStatus(status);
        appSite.setUpdateTime(DateUtils.getNowDate());
        appSite.setUpdateBy(SecurityUtils.getUsername());
        int res = appSiteMapper.updateAppSite(appSite);
        if (res > 0) {
            this.updateSiteCacheList();
        }
        return res;
    }

    /**
     * 批量删除知识关卡
     *
     * @param ids 需要删除的知识关卡主键
     * @return 结果
     */
    @Override
    public int deleteAppSiteByIds(Long[] ids) {
        int res = appSiteMapper.deleteAppSiteByIds(ids);
        if (res > 0) {
            this.updateSiteCacheList();
        }
        return res;
    }

    /**
     * 删除知识关卡信息
     *
     * @param id 知识关卡主键
     * @return 结果
     */
    @Override
    public int deleteAppSiteById(Long id) {
        int res = appSiteMapper.deleteAppSiteById(id);
        if (res > 0) {
            this.updateSiteCacheList();
        }
        return res;
    }
}
